--行会主殿功能
--#include "data\config\guild\Guild.lua" once


function SendGuildRegBag( sysarg, allRedBagYuanBao, allRedBagNum )
	--print("SendGuildRegBag, allRedBagYuanBao="..allRedBagYuanBao..", allRedBagNum="..allRedBagNum)
	local guildId = Actor.getGuildId(sysarg)
	if guildId <= 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0039, ttFlyTip)
		return
	end

	local guildLevel = GetGuildLevel(guildId)
	if guildLevel < 1 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0039, ttFlyTip)
		return
	end

	if allRedBagYuanBao < allRedBagNum then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.GuildRedBag003, ttFlyTip)
		return
	end

	if allRedBagYuanBao < GuildConfig.global.redBagYuanBaoMin then
		Actor.sendTipmsg(sysarg, string.format(Lang.ScriptTips.GuildRedBag011, GuildConfig.global.redBagYuanBaoMin), ttFlyTip)
		return
	end

	if allRedBagNum < GuildConfig.global.redBagCountMin then
		Actor.sendTipmsg(sysarg, string.format( Lang.ScriptTips.GuildRedBag004, GuildConfig.global.redBagCountMin ), ttFlyTip)
		return
	end

	if allRedBagNum > GuildConfig.global.redBagCountMax then
		Actor.sendTipmsg(sysarg, string.format( Lang.ScriptTips.GuildRedBag005, GuildConfig.global.redBagCountMax ), ttFlyTip)
		return
	end

	local myYuanBao = Actor.getMoneyCount(sysarg, mtYuanbao)
	if myYuanBao < allRedBagYuanBao then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0009, ttFlyTip)
		return
	end

	if not Actor.delMoney( sysarg, mtYuanbao, allRedBagYuanBao, GameLog.clGuildRedBagSend, Lang.ScriptTips.GuildLog003 ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0009, ttFlyTip)
		return
	end

	local redbagIdx = Guild.addGuildRedBag( sysarg, allRedBagYuanBao, allRedBagNum )
	Guild.notifyGuildRedBagData( guildId )			--通知行会成员

	local actorId   = Actor.getActorId(sysarg)
	local actorName = Actor.getName(sysarg)
	local msg = string.format( Lang.ScriptTips.GuildRedBag015, actorName, allRedBagYuanBao )
	System.broadcastGuildMsg( guildId, msg )

	System.sendCommonLog( GameLog.clGuildRedBagPut, actorId, actorName , redbagIdx, allRedBagYuanBao, allRedBagNum,
		'','',"send a guild redbag")
end

--[[开启行会BOSS
开启对应的BOSS副本
]]
function GetGuildRedBag( sysarg, redbagIdx )
	--print("GetGuildRedBag, redbagIdx="..redbagIdx)
	if redbagIdx <= 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.GuildRedBag012, ttFlyTip)
		return
	end

	local guildId = Actor.getGuildId(sysarg)
	if guildId <= 0 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0039, ttFlyTip)
		return
	end

	local guildLevel = GetGuildLevel(guildId)
	if guildLevel < 1 then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0039, ttFlyTip)
		return
	end

	local retCode = Guild.canGetGuildRedBag( sysarg, redbagIdx )
	--print("GetGuildRedBag check can, retCode="..retCode)
	if retCode == enGuildRedBagRet_NoYuanbao then			--没有元宝
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.GuildRedBag006, ttFlyTip)
		return
	elseif retCode == enGuildRedBagRet_BagOver then			--红包派完
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.GuildRedBag007, ttFlyTip)
		return
	elseif retCode == enGuildRedBagRet_NoBag then			--红包不存在
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.GuildRedBag010, ttFlyTip)
		return
	elseif retCode == enGuildRedBagRet_TimeOut then			--红包过期
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.GuildRedBag008, ttFlyTip)
		return
	elseif retCode == enGuildRedBagRet_NotGuild then		--非行会成员
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.GuildRedBag009, ttFlyTip)
		return
	elseif retCode == enGuildRedBagRet_HadGot then			--已经领取过
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.GuildRedBag002, ttFlyTip)
		return
	elseif retCode == enGuildRedBagRet_UnKnown then			--未知错误
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0041, ttFlyTip)
		return
	end

	if retCode ~= enGuildRedBagRet_OK then
		return
	end


	local yuanbaoLast, redBagLast = Guild.getGuildRedBagYBAndNum( guildId, redbagIdx, yuanbaoLast, redBagLast )
	local getRedBagYuanBao = GetGuildRedBagYuanBaoNum( sysarg, yuanbaoLast, redBagLast )
	--print("GetGuildRedBag, getRedBagYuanBao="..getRedBagYuanBao)
	if getRedBagYuanBao > 0 then
		local retCode = Guild.getGuildRedBag( sysarg, redbagIdx, getRedBagYuanBao )
		if retCode == enGuildRedBagRet_OK then
			Actor.addMoney( sysarg, mtYuanbao, getRedBagYuanBao, GameLog.clGuildRedBagGet, Lang.ScriptTips.GuildLog004 )
			Actor.sendTipmsg(sysarg, string.format( Lang.ScriptTips.GuildRedBag016, getRedBagYuanBao ), ttFlyTip)

			local actorId   = Actor.getActorId(sysarg)
			local actorName = Actor.getName(sysarg)
			System.sendCommonLog( GameLog.clGuildRedBagTake, actorId, actorName , redbagIdx, yuanbaoLast, getRedBagYuanBao,
				'','',"take a guild redbag")
		end
	end
	--[[
	local yuanbaoLast = 1000
	local redBagNum	  = 10
	local redBagLast  = redBagNum

	for i=1, redBagNum do
		local getRedBagYuanBao = GetGuildRedBagYuanBaoNum( sysarg, yuanbaoLast, redBagLast )
		print("GetGuildRedBagYuanBaoNum, getRedBagYuanBao="..getRedBagYuanBao)
		yuanbaoLast = yuanbaoLast - getRedBagYuanBao
		redBagLast = redBagLast -1
	end
	]]
end

function GetGuildRedBagYuanBaoNum( sysarg, yuanbaoLast, redbagLast )
	--print("GetGuildRedBagYuanBaoNum, yuanbaoLast="..yuanbaoLast..", redbagLast="..redbagLast)
	if yuanbaoLast <=0 or redbagLast <= 0 then
		return 0
	end

	local getRedbagYuanBao = 0
	if redbagLast == 1 then						--只剩下最后1个红包
		getRedbagYuanBao = yuanbaoLast
	else
		local yuanbaoCanGet = yuanbaoLast - (redbagLast-1)*1 			--剩余的元宝至少确保每个红包1个元宝
		local yuanbaoAve	= math.floor( yuanbaoLast / redbagLast )	--元宝均值
		local randLimit		= yuanbaoCanGet * 10 						--扩大随机区间，以便数值精确
		local randNum 		= System.getRandomNumber(randLimit) + 1
		local mainAve		= yuanbaoAve * 10
		local diff			= randNum - mainAve
		local rate 			= diff / (randLimit-mainAve)
		local rate2			= rate * rate * rate
		local diffValue		= math.floor( rate2*yuanbaoCanGet)	--
		getRedbagYuanBao 	= math.floor( yuanbaoAve +  diffValue )
		--[[
		if diff >= 0 then
			getRedbagYuanBao = math.floor( yuanbaoAve +  diffValue )
		else
			getRedbagYuanBao =  math.ceil( yuanbaoAve - diffValue )
		end
		]]

		local halfYuanbaoLast = math.floor( yuanbaoCanGet / 2 )
		if getRedbagYuanBao > halfYuanbaoLast then
			getRedbagYuanBao = halfYuanbaoLast
		end
		--print("-->, yuanbaoCanGet="..yuanbaoCanGet..", randNum="..randNum..", mainAve="..mainAve)
		--print("  --->, diff="..diff..", rate="..rate..", rate2="..rate2..", diffValue="..diffValue..", getYuanBao="..getRedbagYuanBao)
	end
	if getRedbagYuanBao > yuanbaoLast then
		getRedbagYuanBao = yuanbaoLast
	end
	return getRedbagYuanBao
end
